﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;

namespace RoadTest
{
    class InterSection : Grid
    {
        private TrafficLightStrategyFactory trafficLightStrategyFactory;
        private Lane[] lanes = new Lane[8];
            
        public InterSection()
        {
            this.Initialized += new EventHandler(InterSection_Initialized);
        }

        public static readonly DependencyProperty TypeProperty = DependencyProperty.Register("Type",
typeof(IntersectionType), typeof(InterSection), new UIPropertyMetadata((IntersectionType)IntersectionType.SIMPLE));

        public IntersectionType Type
        {
            get
            {
                return (IntersectionType)this.GetValue(TypeProperty);
            }
            set
            {
                this.SetValue(TypeProperty, value);
            }
        }

        void InterSection_Initialized(object sender, EventArgs e)
        {
            createRows();
            createColumns();
            createLanes();
            trafficLightStrategyFactory = new TrafficLightStrategyFactory();
            iTrafficLightStrategy strategy = trafficLightStrategyFactory.createSimpleStrategy();
            strategy.start(lanes);
        }

        private void createRows()
        {
            GridLengthConverter glc = new GridLengthConverter();

            RowDefinition rd1 = new RowDefinition();
            rd1.SetValue(RowDefinition.HeightProperty, (GridLength)glc.ConvertFromString("200"));
            RowDefinition rd2 = new RowDefinition();
            rd2.SetValue(RowDefinition.HeightProperty, (GridLength)glc.ConvertFromString("100"));
            RowDefinition rd3 = new RowDefinition();
            rd3.SetValue(RowDefinition.HeightProperty, (GridLength)glc.ConvertFromString("100"));
            RowDefinition rd4 = new RowDefinition();
            rd4.SetValue(RowDefinition.HeightProperty, (GridLength)glc.ConvertFromString("200"));
            RowDefinitions.Add(rd1);
            RowDefinitions.Add(rd2);
            RowDefinitions.Add(rd3);
            RowDefinitions.Add(rd4);
        }

        private void createColumns()
        {
            GridLengthConverter glc = new GridLengthConverter();

            ColumnDefinition cd1 = new ColumnDefinition();
            cd1.SetValue(ColumnDefinition.WidthProperty, (GridLength)glc.ConvertFromString("200"));
            ColumnDefinition cd2 = new ColumnDefinition();
            cd2.SetValue(ColumnDefinition.WidthProperty, (GridLength)glc.ConvertFromString("100"));
            ColumnDefinition cd3 = new ColumnDefinition();
            cd3.SetValue(ColumnDefinition.WidthProperty, (GridLength)glc.ConvertFromString("100"));
            ColumnDefinition cd4 = new ColumnDefinition();
            cd4.SetValue(ColumnDefinition.WidthProperty, (GridLength)glc.ConvertFromString("200"));
            ColumnDefinitions.Add(cd1);
            ColumnDefinitions.Add(cd2);
            ColumnDefinitions.Add(cd3);
            ColumnDefinitions.Add(cd4);
        }

        private void createLanes()
        {
            Direction[] directions = new Direction[3];
            directions[0] = Direction.FORWARD;
            directions[1] = Direction.LEFT;
            directions[2] = Direction.RIGHT;

            Lane l1 = new Lane();
            l1.SetValue(Lane.OrientationProperty, 180);
            Children.Add(l1);
            Grid.SetRow(l1, 0);
            Grid.SetColumn(l1, 1);
            l1.setTrafficLight(this);
            lanes[0] = l1;
            l1.SetValue(Lane.AllowedDirectionsProperty, directions);

            Lane l2 = new Lane();
            l2.SetValue(Lane.OrientationProperty, 0);
            Children.Add(l2);
            Grid.SetRow(l2, 0);
            Grid.SetColumn(l2, 2);
            lanes[1] = l2;
            l2.SetValue(Lane.AllowedDirectionsProperty, directions);

            Lane l3 = new Lane();
            l3.SetValue(Lane.OrientationProperty, 270);
            Children.Add(l3);
            Grid.SetRow(l3, 1);
            Grid.SetColumn(l3, 3);
            l3.setTrafficLight(this);
            lanes[2] = l3;
            l3.SetValue(Lane.AllowedDirectionsProperty, directions);

            Lane l4 = new Lane();
            l4.SetValue(Lane.OrientationProperty, 90);
            Children.Add(l4);
            Grid.SetRow(l4, 2);
            Grid.SetColumn(l4, 3);
            lanes[3] = l4;
            l4.SetValue(Lane.AllowedDirectionsProperty, directions);

            Lane l5 = new Lane();
            l5.SetValue(Lane.OrientationProperty, 0);
            Children.Add(l5);
            Grid.SetRow(l5, 3);
            Grid.SetColumn(l5, 2);
            l5.setTrafficLight(this);
            lanes[4] = l5;
            l5.SetValue(Lane.AllowedDirectionsProperty, directions);

            Lane l6 = new Lane();
            l6.SetValue(Lane.OrientationProperty, 180);
            Children.Add(l6);
            Grid.SetRow(l6, 3);
            Grid.SetColumn(l6, 1);
            lanes[5] = l6;
            l6.SetValue(Lane.AllowedDirectionsProperty, directions);

            Lane l7 = new Lane();
            l7.SetValue(Lane.OrientationProperty, 90);
            Children.Add(l7);
            Grid.SetRow(l7, 2);
            Grid.SetColumn(l7, 0);
            l7.setTrafficLight(this);
            lanes[6] = l7;
            l7.SetValue(Lane.AllowedDirectionsProperty, directions);

            Lane l8 = new Lane();
            l8.SetValue(Lane.OrientationProperty, 270);
            Children.Add(l8);
            Grid.SetRow(l8, 1);
            Grid.SetColumn(l8, 0);
            lanes[7] = l7;
            l8.SetValue(Lane.AllowedDirectionsProperty, directions);
        }
    }
}
